/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isValidBST = function(root) {
    if(!root)return true;
    let res = Number.NEGATIVE_INFINITY,
        cur = root,
        stack = [root];

    while(stack.length>0){

        cur = cur.left;
        while(cur){
            stack.push(cur);
            cur = cur.left;
        }

        cur = stack.pop();
        if(res>=cur.val)return false;
        res = cur.val;

        cur = cur.right;

        while(!cur&&stack.length>0){
            cur = stack.pop();
            if(res>=cur.val)return false;
            res = cur.val;
            cur = cur.right;
        }

        if(cur)stack.push(cur);
    }

    return true;
};



function TreeNode(val) {
     this.val = val;
     this.left = this.right = null;
}
console.log(isValidBST(new TreeNode(0)));